<template>
  <el-card>
    <span slot="header">添加账号</span>
    <el-form
      :model="formData"
      label-width="80px"
      class="form"
      :rules="rules"
      size="small"
      status-icon
      ref="formRef"
    >
      <!-- 账号 -->
      <el-form-item label="账号" prop="account">
        <el-input v-model="formData.account"></el-input>
      </el-form-item>
      <!-- 密码 -->
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password"></el-input>
      </el-form-item>
      <!-- 用户组 -->
      <el-form-item label="用户组" prop="userGroup">
        <el-select placeholder="请选择用户组" v-model="formData.userGroup">
          <el-option label="普通用户" value="普通用户"></el-option>
          <el-option label="超级管理员" value="超级管理员"></el-option>
        </el-select>
      </el-form-item>
      <!-- 添加&重置 -->
      <el-form-item>
        <el-button type="primary" @click="submit">添加</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import {addUserReq,checkRepeatReq} from '@/api/user'

 const checkAccount = async(rule, value, callback) => {
  // console.log('rule :>> ', rule)
  // console.log('value :>> ', value)
  // console.log('callback :>> ', callback)
  //非空验证(!value==value === '')
  if (value === '') {
    callback(new Error('账号不能为空'))
  } else if (!/^[u4e00-\u9fa50-9a-zA-Z]{3,7}$/.test(value)) {
    callback(new Error('请输入由中文、数字、英文组成的3-7为字符'))
  } else {
    //验证代码是否重复
    let res=await checkRepeatReq({
      account:value,
    })
    let {code,msg}=res.data
    if(code===200){
      callback()
    }else{
       callback(new Error(msg))
    }
    
  }
}
const checkPassword = (rule, value, callback) => {
  // console.log('rule :>> ', rule)
  // console.log('value :>> ', value)
  // console.log('callback :>> ', callback)
  if (value === '') {
    callback(new Error('密码不能为空'))
  } else if (!/^[0-9a-zA-Z_-]{6,10}$/.test(value)) {
    callback(new Error('请输入由数字、英文、英文下划线及-组成的6-10为字符'))
  } else {
    callback()
  }
}
export default {
  data() {
    return {
      formData: {
        account: '',
        password: '',
        userGroup: '',
      },
      rules: {
        account: [{validator: checkAccount, trigger: 'blur'}],
        password: [{validator: checkPassword, trigger: 'blur'}],
        userGroup: [{required: true, message: '请选择用户身份'}],
      },
    }
  },
  methods: {
    // ...addUserReq('user',['addUserReq']),
    //提交表单
    submit() {
      this.$refs.formRef.validate(async valid => {
        // console.log('valid :>> ', valid)
        if (valid) {
          let res = await addUserReq(this.formData)
          let {code, msg} = res.data
          if (code === 0) {
            //跳转
            this.$router.push('/account')
          }
        }
      })
    },
    //重置表单
    reset() {
      this.$refs.formRef.resetFields()
    },
  },
}
</script>

<style lang="scss" scoped>
// .form {
//   width: 350px;
// }
:deep(.el-input) {
  width: 310px;
}
</style>
